<!--
 * @Date: 2022-04-15 15:00:57
 * @LastEditors: wuyuxin
 * @LastEditTime: 2022-04-16 11:38:56
 * @FilePath: \Vue3-ts-cms\src\views\login\components\login-account.vue
 * @description: 文件描述
-->
<template>
  <el-form :model="userData" :rules="rules" label-width="84px" ref="formData">
    <el-form-item label="用户名" prop="userName">
      <el-input
        v-model="userData.userName"
        placeholder="请输入用户名"
        class="input-class"
      />
    </el-form-item>
    <el-form-item label="密 码" prop="password">
      <el-input
        v-model="userData.password"
        placeholder="请输入密码"
        class="input-class"
      />
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { defineComponent, ref, reactive } from 'vue'
import { rules } from '../config/account-config'
import { ElForm } from 'element-plus'
import { useStore } from 'vuex'
import localCache from '@/utils/cache'

export default defineComponent({
  setup() {
    const userData = reactive({
      userName: localCache.getCache('userName') ?? '',
      password: localCache.getCache('password') ?? ''
    })
    const store = useStore()
    const formData = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword?: boolean) => {
      formData.value?.validate((valid) => {
        if (valid) {
          // 1.判断是否需要记住密码
          if (isKeepPassword) {
            // 本地缓存
            localCache.setCache('userName', userData.userName)
            localCache.setCache('password', userData.password)
            localCache.setCache('isKeepPassword', isKeepPassword)
          } else {
            localCache.deleteCache('userName')
            localCache.deleteCache('password')
            localCache.deleteCache('isKeepPassword')
          }
          store.dispatch('loginModule/handleLoginAction', { ...userData })
        }
      })
    }
    return {
      userData,
      rules,
      formData,
      loginAction
    }
  }
})
</script>

<style scoped lang="less">
.input-class {
  width: 250px;
}
</style>
